import os

'''
    LIBS：指定链接的库文件，比如要链接 libpthread.so，则写为 LIBS=['pthread']
    LIBPATH：库文件搜索路径，例如 LIBPATH=['/usr/local/lib/','.']
    CPPPATH：include文件包含路径，例如CPPPATH = ['/home/project/inc']
    CPPDEFINES：宏定义，例如CPPDEFINES='DEBUG'
    CC：编译器，默认情况下scons会根据源文件后缀自动判断。
    CFLAGS：编译参数，例如CFLAGS = '-g -W'
    LDFLAG：链接参数，例如LDFLAG = '-g'
'''

current_path = os.getcwd() # 获取当前路径

build_name = "module_test"
build_dir = os.path.join(current_path, "build") # 编译后文件的保存路径

headers = Split("./ driver/inc ../module1/inc ../module2/inc") # 头文件
libs = ["module1", "module2"] # 库文件

source_files = Glob("*.c")
source_files += Glob("driver/src/*.c")

include_paths = [current_path]
include_paths += [os.path.join(current_path, "driver/inc")]
include_paths += [os.path.join(os.path.dirname(current_path), "module1/inc")]
include_paths += [os.path.join(os.path.dirname(current_path), "module2/inc")]

defines = ['DEBUG','OK']

# abs_headers = [] # 头文件的绝对引用路径
abs_libs = [os.sep.join([build_dir, "a"]), os.sep.join([build_dir, "so"])] # 库的绝对引用路径

# for item in headers:
#     abs_item = os.path.join(current_path, item)
#     abs_headers.append(abs_item)

# for item in libs:
#     abs_item = os.path.join(current_path, item)
#     abs_libs.append(abs_item)

ccflags = ['-g','-W']

env = Environment(CC = 'gcc')
env.Append(CPPPATH=include_paths) # 要编译的C头文件路径
env.Append(CPPDEFINES=defines) # 指定预编译选项
env.Append(LIBPATH=abs_libs)
env.Append(LIBS=libs)
env.Append(CCCOMSTR="CC $SOURCES")
env.Append(LINKCOMSTR="LINK $TARGET")
env.Append(CCFLAGS=ccflags)

env["headers"]   = headers
env["libs"]      = libs
env["ccflags"]   = ccflags
env['build_dir'] = build_dir

Export('env') # SConstruct 和 SConscript 共享变量使用的就是 env 这个变量

SConscript(["../module1/SConscript"], variant_dir='build/release1', src_dir=os.path.join(os.path.dirname(os.getcwd()), "module1"), duplicate=1)
SConscript(["../module2/SConscript"], variant_dir='build/release2', src_dir=os.path.join(os.path.dirname(os.getcwd()), "module2"), duplicate=1)

env.Program(target = os.path.join(build_dir, build_name), source = source_files)

print("\n All Done, Please Check {0}".format(env['build_dir']))